package com.xyy.ec.pop.erp.hx.helper;

import com.google.common.collect.Lists;
import com.xyy.ec.pop.erp.hx.config.HXTaskConfig;
import com.xyy.ec.pop.erp.hx.po.hx.CustomerHXPo;
import com.xyy.ec.pop.erp.hx.po.hx.ProductFullHXPo;
import com.xyy.ec.pop.erp.hx.po.hx.ProductStockHXPo;
import com.xyy.ec.pop.erp.hx.po.pop.dto.PopErpClientInfoDto;
import com.xyy.ec.pop.erp.hx.po.pop.dto.PopErpSkuForInsertDto;
import com.xyy.ec.pop.erp.hx.po.pop.dto.PopErpSkuStockV2Dto;
import com.xyy.ec.pop.erp.hx.utils.DateUtils;
import com.xyy.ec.pop.erp.hx.utils.ReflectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author shiyujie
 * @date 2022/06/06 21:20
 */
public class HXHelper {

    public static List<PopErpSkuForInsertDto> convertToPopErpSkuDtos(List<ProductFullHXPo> rows, Map<Object, List<ProductStockHXPo>> stockMap, HXTaskConfig config) {
        if (CollectionUtils.isEmpty(rows)) {
            return Lists.newArrayList();
        }
        List<PopErpSkuForInsertDto> popErpSkuForInsertDtolist = Lists.newArrayList();
        for (ProductFullHXPo productFullHXPo : rows) {
            popErpSkuForInsertDtolist.add(convertFromProductFullHXPo(productFullHXPo, stockMap, config));
        }
        return popErpSkuForInsertDtolist;
    }

    private static PopErpSkuForInsertDto convertFromProductFullHXPo(ProductFullHXPo productFullHXPo, Map<Object, List<ProductStockHXPo>> stockMap, HXTaskConfig config) {
        PopErpSkuForInsertDto popErpSkuForInsertDto = new PopErpSkuForInsertDto();
        popErpSkuForInsertDto.setErpCode(productFullHXPo.getCode());
        popErpSkuForInsertDto.setProductName(productFullHXPo.getName());
        popErpSkuForInsertDto.setCommonName(productFullHXPo.getAlias());
        popErpSkuForInsertDto.setApprovalNumber(productFullHXPo.getPermitcode());
        popErpSkuForInsertDto.setCode(productFullHXPo.getBarcode());
        popErpSkuForInsertDto.setManufacturer(productFullHXPo.getFactory());
        // 全量同步，中包装默认为1
        popErpSkuForInsertDto.setMediumPackageNum(1);
        // 中包装单位和换算比例一一对应,unit1对应urate1, unit2对应urate2，unit3对应urate3
        popErpSkuForInsertDto.setProductUnit(String.valueOf(ReflectionUtils.getFieldValue(productFullHXPo, config.getUnit())));
        popErpSkuForInsertDto.setSpec(productFullHXPo.getStandard());
        popErpSkuForInsertDto.setDosageForm(productFullHXPo.getMt_name());
        popErpSkuForInsertDto.setTerm(productFullHXPo.getValidmonth() + "月");
        popErpSkuForInsertDto.setSuggestPrice(null);
        popErpSkuForInsertDto.setProductionArea(productFullHXPo.getMakearea());
        popErpSkuForInsertDto.setMonomerPrice(BigDecimal.valueOf(999));
        popErpSkuForInsertDto.setChainPrice(BigDecimal.valueOf(999));
        List<ProductStockHXPo> productStockHXPos = stockMap.get("code".equals(config.getNumberType()) ? productFullHXPo.getCode() : productFullHXPo.getPid());
        if (CollectionUtils.isEmpty(productStockHXPos)) {
            return new PopErpSkuForInsertDto();
        }
        if (!CollectionUtils.isEmpty(productStockHXPos)) {
            Date minProductDate = productStockHXPos.stream().map(m -> m.getProductdate()).filter(Objects::nonNull).min(Date::compareTo).get();
            Date maxProductDate = productStockHXPos.stream().map(m -> m.getProductdate()).filter(Objects::nonNull).max(Date::compareTo).get();
            popErpSkuForInsertDto.setOldestProDate(DateUtils.format(minProductDate, DateUtils.SHORT_DATE_FORMAT_STR));
            popErpSkuForInsertDto.setNewProDate(DateUtils.format(maxProductDate, DateUtils.SHORT_DATE_FORMAT_STR));
            Date minValidDate = productStockHXPos.stream().map(m -> m.getValiddate()).filter(Objects::nonNull).min(Date::compareTo).get();
            Date maxValidDate = productStockHXPos.stream().map(m -> m.getValiddate()).filter(Objects::nonNull).max(Date::compareTo).get();
            popErpSkuForInsertDto.setNearEffect(DateUtils.format(minValidDate, DateUtils.SHORT_DATE_FORMAT_STR));
            popErpSkuForInsertDto.setFarEffect(DateUtils.format(maxValidDate, DateUtils.SHORT_DATE_FORMAT_STR));
            int qyt = productStockHXPos.stream().mapToInt(ProductStockHXPo::getQuantity).filter(Objects::nonNull).sum();
            popErpSkuForInsertDto.setAvailableQyt(qyt);
        }
        if (popErpSkuForInsertDto.getAvailableQyt() > 0) {
            return popErpSkuForInsertDto;
        } else {
            return new PopErpSkuForInsertDto();
        }
    }

    public static List<PopErpClientInfoDto> convertToPopErpClientInfoDtos(List<CustomerHXPo> rows) {
        if (CollectionUtils.isEmpty(rows)) {
            return Lists.newArrayList();
        }
        List<PopErpClientInfoDto> popErpClientInfoDtolist = Lists.newArrayList();
        for (CustomerHXPo customerHXPo : rows) {
            popErpClientInfoDtolist.add(convertFromCustomerHXPo(customerHXPo));
        }
        return popErpClientInfoDtolist;
    }

    private static PopErpClientInfoDto convertFromCustomerHXPo(CustomerHXPo customerHXPo) {
        PopErpClientInfoDto popErpClientInfoDto = new PopErpClientInfoDto();
        popErpClientInfoDto.setCustomerId(String.valueOf(customerHXPo.getCid()));
        popErpClientInfoDto.setCustomerCode(customerHXPo.getCode());
        String customerName = customerHXPo.getName().replaceAll("[a-zA-Z]", "");
        popErpClientInfoDto.setCustomerName(customerName);
        return popErpClientInfoDto;
    }

    public static List<PopErpSkuStockV2Dto> convertToPopErpSkuStockDtos(Map<String, List<ProductStockHXPo>> pidStockMap) {
        if (MapUtils.isEmpty(pidStockMap)) {
            return Lists.newArrayList();
        }
        List<PopErpSkuStockV2Dto> popErpClientInfoDtolist = Lists.newArrayList();
        for (Map.Entry<String, List<ProductStockHXPo>> entry : pidStockMap.entrySet()) {
            popErpClientInfoDtolist.add(convertFromProductStockHXPoEnrty(entry.getKey(), entry.getValue()));
        }
        return popErpClientInfoDtolist;
    }

    private static PopErpSkuStockV2Dto convertFromProductStockHXPoEnrty(String erpCode, List<ProductStockHXPo> productStockHXPos) {
        PopErpSkuStockV2Dto popErpSkuStockDto = new PopErpSkuStockV2Dto();
        popErpSkuStockDto.setErpCode(erpCode);
        Date minProductDate = productStockHXPos.stream().map(m -> m.getProductdate()).filter(Objects::nonNull).min(Date::compareTo).get();
        Date maxProductDate = productStockHXPos.stream().map(m -> m.getProductdate()).filter(Objects::nonNull).max(Date::compareTo).get();
        popErpSkuStockDto.setOldestProDate(DateUtils.format(minProductDate, DateUtils.SHORT_DATE_FORMAT_STR));
        popErpSkuStockDto.setNewProDate(DateUtils.format(maxProductDate, DateUtils.SHORT_DATE_FORMAT_STR));
        Date minValidDate = productStockHXPos.stream().map(m -> m.getValiddate()).filter(Objects::nonNull).min(Date::compareTo).get();
        Date maxValidDate = productStockHXPos.stream().map(m -> m.getValiddate()).filter(Objects::nonNull).max(Date::compareTo).get();
        popErpSkuStockDto.setNearEffect(DateUtils.format(minValidDate, DateUtils.SHORT_DATE_FORMAT_STR));
        popErpSkuStockDto.setFarEffect(DateUtils.format(maxValidDate, DateUtils.SHORT_DATE_FORMAT_STR));
        Integer stockTotal = productStockHXPos.stream().map(m -> m.getQuantity()).filter(Objects::nonNull).reduce(Integer::sum).get();
        popErpSkuStockDto.setAvailableQyt(stockTotal);
        return popErpSkuStockDto;
    }


}
